package erlwitch.gui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import erlwitch.ErlwitchPlugin;

public class ProjectView extends JScrollPane {

	private static final long serialVersionUID = 1L;
	
	private JTextField name;
	private JTextField compiler;
	private JTextField source;
	private JTextField beam;
	private JCheckBox update;
	
	private JButton bNew;
	private JButton bSave;
	private JButton bLoad;

	public ProjectView() {
		JPanel innerPanel = new JPanel(new GridBagLayout());
		add(innerPanel);
		getViewport().setView(innerPanel);
		innerPanel.add(new JLabel("Project name:"), simpleConstraint(0,0,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE));
        name = new JTextField(10);
        name.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent e) {
			}

			public void focusLost(FocusEvent e) {
				String text = name.getText().trim();
				ErlwitchPlugin.getProject().setName(text);
				ErlwitchPlugin.getProject().setDirty(true);
				name.setText(text);
			}
        	
        });
        name.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String text = name.getText().trim();
				ErlwitchPlugin.getProject().setName(text);
				ErlwitchPlugin.getProject().setDirty(true);
				name.setText(text);
			}
        	
        });
        innerPanel.add(name, simpleConstraint(1,0,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH));
        innerPanel.add(new JLabel("Compiler path:"), simpleConstraint(0,1,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE));
        compiler = new JTextField(10);
        compiler.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent e) {
			}

			public void focusLost(FocusEvent e) {
				String text = compiler.getText().trim();
				ErlwitchPlugin.getProject().setCompilerPath(new File(text));
				ErlwitchPlugin.getProject().setDirty(true);
				compiler.setText(text);
			}
        	
        });
        compiler.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String text = compiler.getText().trim();
				ErlwitchPlugin.getProject().setCompilerPath(new File(text));
				ErlwitchPlugin.getProject().setDirty(true);
				compiler.setText(text);
			}
        	
        });
        innerPanel.add(compiler, simpleConstraint(1,1,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH));
        innerPanel.add(new JLabel("Source root:"), simpleConstraint(0,2,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE));
        source = new JTextField(10);
        source.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent e) {
			}

			public void focusLost(FocusEvent e) {
				String text = source.getText().trim();
				ErlwitchPlugin.getProject().setSourceRootPath(new File(text));
				ErlwitchPlugin.getProject().setDirty(true);
				source.setText(text);
			}
        	
        });
        source.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String text = source.getText().trim();
				ErlwitchPlugin.getProject().setSourceRootPath(new File(text));
				ErlwitchPlugin.getProject().setDirty(true);
				source.setText(text);
			}
        	
        });
        innerPanel.add(source, simpleConstraint(1,2,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH));
        innerPanel.add(new JLabel("Beam root:"), simpleConstraint(0,3,0,0,GridBagConstraints.WEST,GridBagConstraints.NONE));
        beam = new JTextField(10);
        beam.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String text = beam.getText().trim();
				ErlwitchPlugin.getProject().setBeamRootPath(new File(text));
				ErlwitchPlugin.getProject().setDirty(true);
				beam.setText(text);
			}
        	
        });
        innerPanel.add(beam, simpleConstraint(1,3,1,0,GridBagConstraints.CENTER,GridBagConstraints.BOTH));
        update = new JCheckBox("Update versions on build");
        update.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				boolean checked = update.isSelected();
				ErlwitchPlugin.getProject().setDirty(true);
				ErlwitchPlugin.getProject().setUpdateVersions(checked);
			}
        	
        });
        innerPanel.add(update, fatConstraint(0,4,2,1,0,GridBagConstraints.WEST,GridBagConstraints.NONE));
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        bNew = new JButton("New");
        bSave = new JButton("Save");
        bLoad = new JButton("Load");
        buttonPanel.add(bNew);
        buttonPanel.add(bSave);
        buttonPanel.add(bLoad);
        innerPanel.add(buttonPanel, fatConstraint(0,5,2,1,0,GridBagConstraints.WEST,GridBagConstraints.NONE));
	}

	private GridBagConstraints simpleConstraint(int x, int y, double weightx, double weighty, int anchor, int fill) {
		GridBagConstraints c = new GridBagConstraints();
        c.gridx = x;
        c.gridy = y;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = weightx;
        c.weighty = weighty;
        c.anchor = anchor;
        c.fill = fill;
        return c;
	}
	
	private GridBagConstraints fatConstraint(int x, int y, int cols, double weightx, double weighty, int anchor, int fill) {
		GridBagConstraints c = new GridBagConstraints();
        c.gridx = x;
        c.gridy = y;
        c.gridwidth = cols;
        c.gridheight = 1;
        c.weightx = weightx;
        c.weighty = weighty;
        c.anchor = anchor;
        c.fill = fill;
        return c;
	}
}
